package com.cdd.quartz;

import org.quartz.*;

import java.util.Date;

@DisallowConcurrentExecution //禁止并发执行
@PersistJobDataAfterExecution  // JobDataMap数据持久化
public class MyJob implements Job {

//    private String name;
//
//    public void setName(String name) {
//        this.name = name;
//    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
//        // 获取到jobDetail中携带的数据
//        JobDataMap jobDetailMap = context.getJobDetail().getJobDataMap();
//
//        // 获取触发器携带的数据
//        JobDataMap triggerMap = context.getTrigger().getJobDataMap();
//
//        // 获取合并携带的数据（若key相同会覆盖）
//        JobDataMap mergeMap = context.getMergedJobDataMap();
//
//        System.out.println("jobDetailMap:" + jobDetailMap.getString("job"));
//        System.out.println("triggerMap:" + triggerMap.getString("trigger"));
//        System.out.println("mergeMap:" + mergeMap.getString("trigger"));

        // --------------------------------------------------------------------
        // 加上属性name和其set方法后，携带的数据会自动调用set方法赋值
//        System.out.println("name："  + name);

        // --------------------------------------------------------------------
        // 每个任务执行时，都是不同的实例，hashcode也不相同
//        System.out.println("jobDetail:" + System.identityHashCode(context.getJobDetail()));
//        System.out.println("job:" + System.identityHashCode(context.getJobInstance()));

        // --------------------------------------------------------------------
//        System.out.println("execute：" + new Date());
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        // --------------------------------------------------------------------
        JobDataMap triggerMap = context.getJobDetail().getJobDataMap();
        triggerMap.put("count", triggerMap.getInt("count") + 1);
        System.out.println("triggerMap count：" + triggerMap.get("count"));

        boolean is = context.isRecovering();
    }
}
